package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import conexao.ConexaoSQLServer;
import modelo.Cliente;
import modelo.Produto;


public class ClienteDao implements ICantinaDao {
	
	Connection con = ConexaoSQLServer.getConnection();
	
	@Override
	public void gravar(Object object) {
		String sql = "INSERT INTO CLIENTE" + " VALUES (?,?,?,?,?,?)";
		Cliente cliente = (Cliente) object;
		PreparedStatement pstm = null;
		try {
			pstm = con.prepareStatement(sql);
			pstm.setString(1, cliente.getNome());
			pstm.setString(2, cliente.getTelefone());
			pstm.setString(3, cliente.getEndereco());
			pstm.setString(4, cliente.getEmail());
			pstm.setString(5, cliente.getFuncao());
			pstm.setInt(6, cliente.getCodCarteira());
			pstm.execute();
	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				pstm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void atualizar(Object object) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void deletar(int id) {
		String sql = "DELETE CLIENTE WHERE ID = ?";
		PreparedStatement pstm = null;
		try {
			pstm = con.prepareStatement(sql);
			pstm.setInt(1, id);
			pstm.execute();
			pstm.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				pstm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public Object buscarCod(int id) {
		String sql = "SELECT * FROM CLIENTE WHERE ID = ?";
		Cliente cliente = new Cliente();
		PreparedStatement pstm = null;
		try {
			pstm = con.prepareStatement(sql);
			pstm.setInt(1, id);
			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				cliente.setId(rs.getInt(1));
				cliente.setNome(rs.getString(2));
				cliente.setTelefone(rs.getString(3));
				cliente.setEndereco(rs.getString(4));
				cliente.setEmail(rs.getString(5));
				cliente.setFuncao(rs.getString(6));
				cliente.setCodCarteira(rs.getInt(7));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				pstm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cliente;
	}

	@Override
	public Object buscarNome(String nome) {
		String sql = "SELECT * FROM CLIENTE WHERE NOME LIKE '%"+ nome +"%'";
		Cliente cliente = new Cliente();
		PreparedStatement pstm = null;
		try {
			pstm = con.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				cliente.setId(rs.getInt(1));
				cliente.setNome(rs.getString(2));
				cliente.setTelefone(rs.getString(3));
				cliente.setEndereco(rs.getString(4));
				cliente.setEmail(rs.getString(5));
				cliente.setFuncao(rs.getString(6));
				cliente.setCodCarteira(rs.getInt(7));
			}/*else{
				throw new SQLException("Nenhum Produto encontrado por nome");
			}*/
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				pstm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return cliente;
	}

	@Override
	public List<?> listarTodos() {
		String sql = "SELECT * FROM CLIENTE";
		List<Cliente> listaCliente = new ArrayList<Cliente>();
		PreparedStatement pstm = null;
		try {
			pstm = con.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				Cliente cliente = new Cliente();
				
				cliente.setId(rs.getInt(1));
				cliente.setNome(rs.getString(2));
				cliente.setTelefone(rs.getString(3));
				cliente.setEndereco(rs.getString(4));
				cliente.setEmail(rs.getString(5));
				cliente.setFuncao(rs.getString(6));
				cliente.setCodCarteira(rs.getInt(7));
				
				listaCliente.add(cliente);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				pstm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listaCliente;
	}

}
